﻿Imports Abyx.SVCMS.Library.DB
Imports Abyx.SVCMS.Library
Imports Abyx.WindowsExtendedControls

Public Class SQC_CatalogItem_Form
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iFormActionOnData As FormActionOnDataEnum
    Dim iDataObject As CatalogItemType = Nothing
#End Region

#Region "Overloads/Overrides"
    Public Overloads Sub Show(FormActionOnData As FormActionOnDataEnum, _
                              Optional DataObject As CatalogItemType = Nothing)
        Me.Show()
        If DataObject IsNot Nothing Then
            Me.iDataObject = DataObject
            Me.iFormActionOnData = FormActionOnDataEnum.UpdateRecord
        Else
            Me.iDataObject = New CatalogItemType(Guid.NewGuid)
            Me.iFormActionOnData = FormActionOnDataEnum.NewRecord
        End If
        Refresh_ListControls()
        Refresh_ControlsWithDataObject()
    End Sub
#End Region

#Region "Events"
    Private Sub SQC_CatalogItem_Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        InitializeThisFormAndControlSettings()
    End Sub
    Private Sub SQC_CatalogItem_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Features_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Features_ImageButton.Click
        Dim features As New SQC_CatalogItemFeatures_Form
        features.MdiParent = Me.MdiParent
        features.Show(FormActionOnDataEnum.UpdateRecord, Me.iDataObject)
    End Sub
    Private Sub BrandName_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles BrandName_TextBox.TextChanged
        Me.Refresh_FormTitle()
    End Sub
    Private Sub Picture_EdPicureBox_AddImage_Click(sender As System.Object, e As System.EventArgs) Handles Picture_EdPicureBox.AddImage_Click
        If PictureFile_OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Picture_EdPicureBox.Image = Image.FromFile(PictureFile_OpenFileDialog.FileName)
        End If
    End Sub
    Private Sub Commit_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Commit_ImageButton.Click

        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        Me.Refresh_DataObjectWithControls()
        sqcMan.Update_CatalogItem(Me.iDataObject)
        Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.UpdateRecord)
        SVCM_Form.StatusInfoMsgPut("Catalog item has been updated")
        Me.Close()

    End Sub
    Private Sub Cancel_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Cancel_ImageButton.Click
        Me.Close()
    End Sub
#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        If (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is CatalogItemType) Then
            If Me.iDataObject = DataObject Then
                Return Me
            End If
        End If
        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, _
                                               Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.EquipmentCatalog, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, _
                                                 DataTypeCategory As RelatedControlDataCategoriesEnum, _
                                                 Optional DataItem As Object = Nothing, _
                                                 Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.EquipmentCatalog

                If ActionOnDataItem = FormActionOnDataEnum.DeleteRecord Then
                    If (DataItem IsNot Nothing) _
                        AndAlso (TypeOf DataItem Is CatalogItemType) Then
                        Dim dataObject As CatalogItemType = DataItem
                        If dataObject = Me.iDataObject Then
                            AMsg.ShowDialog("The data being edited has been deleted", MsgBoxStyle.Exclamation)
                            Me.Close()
                        End If
                    End If
                End If

            Case Is = RelatedControlDataCategoriesEnum.Manufacturers

                Me.Refresh_ManufacturersDbList()

        End Select

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        With Me.iDataObject
            Me.EquipmentId_TextBox.Text = .EquipmentId.ToString
            Me.EquipmentType_StylishComboBox.SelectedItem = .EquipmentType
            For Each item As StylishComboItem In Me.Manufacturer_StylishComboBox.Items
                Dim man As ManufacturerType = item.DataObject
                If man.ManufacturerId.Equals(.ManufacturerId) Then
                    Me.Manufacturer_StylishComboBox.SelectedItem = item
                    Exit For
                End If
            Next
            Me.ProductModelNo_TextBox.Text = .ProductModelNumber
            Me.BrandName_TextBox.Text = .Name
            Me.Description_TextBox.Text = .Description
            Me.Picture_EdPicureBox.Image = .Picture
        End With
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls
        With Me.iDataObject
            If Me.EquipmentId_TextBox.Text = "" Then
                Me.EquipmentId_TextBox.Text = Guid.NewGuid().ToString
            End If
            .EquipmentId = New Guid(Me.EquipmentId_TextBox.Text)
            .EquipmentType = Me.EquipmentType_StylishComboBox.SelectedItem
            If Manufacturer_StylishComboBox.SelectedItem IsNot Nothing Then
                Dim man As ManufacturerType = Manufacturer_StylishComboBox.SelectedItem.DataObject
                .ManufacturerId = man.ManufacturerId
                .ManufacturerName = man.Name
            End If
            .ProductModelNumber = Me.ProductModelNo_TextBox.Text
            .Name = Me.BrandName_TextBox.Text
            .Description = Me.Description_TextBox.Text
            .Picture = Me.Picture_EdPicureBox.Image
        End With
    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_FormTitle()
        If Me.iDataObject.Name <> "" Then
            Me.Text = "Configure Catalog Item " & Me.iDataObject.Name
        Else
            Me.Text = "Configure Catalog Item"
        End If
    End Sub
    Private Sub Refresh_ListControls()

        Refresh_EquipmentTypesFixedList()

        Refresh_ManufacturersDbList()

    End Sub
    Private Sub Refresh_EquipmentTypesFixedList()

        Dim previousSelectedItem As SQC_EquipmentTypesEnum = EquipmentType_StylishComboBox.SelectedItem
        If EquipmentType_StylishComboBox.SelectedItem IsNot Nothing Then
            previousSelectedItem = EquipmentType_StylishComboBox.SelectedItem
        Else
            previousSelectedItem = SQC_EquipmentTypesEnum.Mobile_Phone
        End If
        EquipmentType_StylishComboBox.Items.Clear()
        Dim selectedIndex As Integer = -1
        For Each iA As SQC_EquipmentTypesEnum In [Enum].GetValues(GetType(SQC_EquipmentTypesEnum))
            If (previousSelectedItem = iA) Then
                selectedIndex = EquipmentType_StylishComboBox.Items.Add(iA)
            Else
                EquipmentType_StylishComboBox.Items.Add(iA)
            End If
        Next
        If EquipmentType_StylishComboBox.Items.Count > 0 Then
            If selectedIndex <> -1 Then
                EquipmentType_StylishComboBox.SelectedIndex = selectedIndex
            Else
                EquipmentType_StylishComboBox.SelectedItem = SQC_EquipmentTypesEnum.Mobile_Phone
            End If
        End If

    End Sub
    Private Sub Refresh_ManufacturersDbList()

        'fetch data
        Dim man As New SqcManager(AppSettings.LoggedUser)
        Dim manufacturers As ManufacturerTypeCollection = man.Get_Manufacturers_All()

        'get previous selection
        Dim prevSelection As ManufacturerType = Nothing
        If Manufacturer_StylishComboBox.SelectedItem IsNot Nothing Then
            Dim cmbItem As StylishComboItem = Manufacturer_StylishComboBox.SelectedItem
            If cmbItem.DataObject IsNot Nothing Then
                prevSelection = cmbItem.DataObject
            End If
        End If


        'clear list
        Manufacturer_StylishComboBox.Items.Clear()

        'addup new list
        Dim foundIndex As Integer = -1
        For i As Integer = 0 To manufacturers.Count - 1
            Dim manufacturer As ManufacturerType = manufacturers.Item(i)
            If (prevSelection IsNot Nothing) _
                AndAlso (prevSelection = manufacturer) Then
                foundIndex = Manufacturer_StylishComboBox.Items.Add(New StylishComboItem(manufacturer.Logo, manufacturer))
            Else
                Manufacturer_StylishComboBox.Items.Add(New StylishComboItem(manufacturer.Logo, manufacturer))
            End If
        Next

        'make default selection
        If Manufacturer_StylishComboBox.Items.Count > 0 Then
            If foundIndex <> -1 Then
                Manufacturer_StylishComboBox.SelectedIndex = foundIndex
            Else
                Manufacturer_StylishComboBox.SelectedIndex = 0
            End If
        End If
    End Sub

#End Region




End Class